package com.event.management.frontend.mapper;

import com.event.management.frontend.domain.UserTicket;
import org.apache.ibatis.annotations.*;

import java.util.List;

/**
 * 用户电子票Mapper接口
 */
@Mapper
public interface UserTicketMapper {
    
    /**
     * 插入电子票记录
     * @param userTicket 电子票对象
     * @return 影响行数
     */
    @Insert("INSERT INTO user_tickets(order_id, user_id, event_id, ticket_id, ticket_code, status, created_at) " +
            "VALUES(#{orderId}, #{userId}, #{eventId}, #{ticketId}, #{ticketCode}, #{status}, #{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int insert(UserTicket userTicket);
    
    /**
     * 批量插入电子票记录
     * @param userTickets 电子票对象列表
     * @return 影响行数
     */
    @Insert("<script>" +
            "INSERT INTO user_tickets(order_id, user_id, event_id, ticket_id, ticket_code, status, created_at) VALUES " +
            "<foreach collection='list' item='ticket' separator=','>" +
            "(#{ticket.orderId}, #{ticket.userId}, #{ticket.eventId}, #{ticket.ticketId}, #{ticket.ticketCode}, #{ticket.status}, #{ticket.createdAt})" +
            "</foreach>" +
            "</script>")
    int batchInsert(List<UserTicket> userTickets);
    
    /**
     * 更新电子票状态
     * @param id 电子票ID
     * @param status 状态
     * @return 影响行数
     */
    @Update("UPDATE user_tickets SET status = #{status} WHERE id = #{id}")
    int updateStatus(@Param("id") Long id, @Param("status") String status);
    
    /**
     * 根据ID查询电子票
     * @param id 电子票ID
     * @return 电子票对象
     */
    @Select("SELECT * FROM user_tickets WHERE id = #{id}")
    UserTicket selectById(Long id);
    
    /**
     * 根据订单ID查询电子票列表
     * @param orderId 订单ID
     * @return 电子票列表
     */
    @Select("SELECT * FROM user_tickets WHERE order_id = #{orderId}")
    List<UserTicket> selectByOrderId(Long orderId);
    
    /**
     * 根据用户ID查询电子票列表
     * @param userId 用户ID
     * @return 电子票列表
     */
    @Select("SELECT * FROM user_tickets WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<UserTicket> selectByUserId(Long userId);
    
    /**
     * 根据票据编号查询电子票
     * @param ticketCode 票据编号
     * @return 电子票对象
     */
    @Select("SELECT * FROM user_tickets WHERE ticket_code = #{ticketCode}")
    UserTicket selectByTicketCode(String ticketCode);
    
    /**
     * 检查电子票是否已回答问卷
     * @param ticketId 电子票ID
     * @return 电子票问卷回答状态
     */
    @Select("SELECT questionnaire_answered FROM user_tickets WHERE id = #{ticketId}")
    Integer checkQuestionnaireAnswered(Long ticketId);
} 